/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
 * catchit
 * Copyright (C) 2013 Ryan Grinkewitz <graphite@Toto>
 * 
 */

#include "game-loop.h"
#include "timer.h"
#include "SDL/SDL.h"


GameLoop::GameLoop(){

	displayManager = DisplayManager::getInstance();	
	eventManager = EventManager::getInstance();
	gameStateManager = GameStateManager::getInstance();
	
	gameStateManager->initializeState();

	agentAI = new QLearning (16384, 3, .1, .1);
	agentAI->readQFunctionTable("test.csv");
}
	
void GameLoop::loopGame(){
	Action currentPlayerAction;
	Action computerAction;
	Timer fps;
	int playerScore = 0;
	int computerScore = 0;

	int gameStartTime;

	displayManager->updateDisplay (gameStateManager->getGameState());

//	while(eventManager->getEventAction() == DO_NOTHING){
//		eventManager->processEvent();
//		currentPlayerAction = eventManager->getEventAction();
//		SDL_Delay(100);
//	};

	gameStartTime = SDL_GetTicks();
	
	while( eventManager->getEventAction() != QUIT ) {
		//std::cout << "Looping.." << std::endl;
		fps.start();
		
		eventManager->processEvent();
		//currentPlayerAction = eventManager->getEventAction();
		currentPlayerAction = eventManager->getRandomAction();
	


		gameStateManager->getGameState()->getPlayer ()
			->processAction (currentPlayerAction);

		
		Computer* computer = gameStateManager->getGameState()->getComputer ();
		if(computer->getIsOnPlatform()){
			int computerState = gameStateManager->getAgentState (computer);
			agentAI->setCurrentState (computerState);
			computerAction = (Action)agentAI->getAction ();
		}	

//		Computer* computer = gameStateManager->getGameState()->getComputer ();

		computer->processAction (computerAction);

		int currentScore = computer->getScore();

		if(computer->getIsOnPlatform()){
			int agentState = gameStateManager->getAgentState (computer);

			if(computer->getYPosition() < 0 && currentScore > computerScore){
				agentAI->updateQFunction(agentState,
		                         -100);			
			}else{
				agentAI->updateQFunction(agentState,
		                         currentScore - computerScore);
			}
		}


		computerScore = currentScore;
		gameStateManager->evaluateWin ();

		if(gameStateManager->getGameState()->getPlayer ()->getYPosition() < 80){
			gameStateManager->updateGamePosition(SCROLL_ACCELERATION*4);
		}else if(gameStateManager->getGameState()->getPlayer ()->getYPosition() 
		        > SCREEN_HEIGHT - SCREEN_HEIGHT/4){
		}else{
			gameStateManager->updateGamePosition(SCROLL_ACCELERATION);
		}
		
		displayManager->updateDisplay (gameStateManager->getGameState());

		if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND  ) { 
			 //Sleep the remaining frame time 
			SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() ); 
		}

		if(gameStateManager->isGameWon() || SDL_GetTicks() > gameStartTime + 180000){
			gameStateManager->resetManager ();
			gameStartTime = SDL_GetTicks();
			playerScore = 0;
			playerScore = 0;
			agentAI->writeQFunctionTable("test.csv");
		}

	}

	agentAI->writeQFunctionTable("test.csv");
	delete agentAI;

}
